package com.example.arthas.controller;

import org.apache.commons.collections.ListUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/arthas")
public class ArthasController {

    /*找 交集 */
    @GetMapping("/test")
    public String test() {
        doSomething();
        return "test";
    }

    /* 并集 */
    @GetMapping("/test2")
    public String test2() {
        HashSet<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3));
        HashSet<Integer> set2 = new HashSet<>(Arrays.asList(2, 3, 4));

        /* 并集 */
        System.out.println("set1.addAll(set2) = " + set1.addAll(set2));/*set1.addAll(set2) = true*/
        System.out.println("set1 = " + set1);/*set1 = [1, 2, 3, 4]*/

        return "test2";
    }

    /*差集*/
    @GetMapping("/test3")
    public String test3() {
        HashSet<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3));
        HashSet<Integer> set2 = new HashSet<>(Arrays.asList(2, 3, 4));

        /*找两个集合的差集*/
        System.out.println("set1.removeAll(set2) = " + set1.removeAll(set2));/*set1.removeAll(set2) = true*/
        System.out.println("set1 = " + set1);/*set1 = [1]*/
        System.out.println("set2 = " + set2);/*set2 = [2, 3, 4]*/

        return "test3";
    }

    @GetMapping("/test4")
    public String test4() {

//        ArrayList<Integer> integers = new ArrayList<>();
//        integers.add(1);
//        integers.add(2);
//        /*复制一个integers*/
//        ArrayList<Integer> integers1 = new ArrayList<>(integers);
//        integers.add(3);
//        System.out.println("integers = " + integers);/*integers = [1, 2, 3]*/
//        System.out.println("integers1 = " + integers1);/*integers1 = [1, 2]*/

        HashSet<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3));

        /*复制 set  并且复制出来的集合和之前的集合是两个集合 而不是简单的地址的指向*/
        HashSet<Integer> set1 = new HashSet<>(set);
        set.add(4);
        System.out.println("set = " + set);/*set = [1, 2, 3, 4]*/
        System.out.println("set1 = " + set1);/*set1 = [1, 2, 3]*/

        return "test4";
    }


    private void doSomething() {
        HashSet<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3));
        HashSet<Integer> set2 = new HashSet<>(Arrays.asList(2, 3, 4));

        /* disjoint(Collection> c1, Collection> c2) 方法返回true表示两个collection中没有相同的元素。 */
        System.out.println("Collections.disjoint(set1, set2) = " +
                Collections.disjoint(set1, set2));

        /* containsAny()方法：判断两个集合是否有交集，如果存在交集则返回true，否则返回false */
        System.out.println("CollectionUtils.containsAny(set1, set2) = " +
                CollectionUtils.containsAny(set1, set2));

        /* 找到两个集合的交集 */
        List intersection = ListUtils.intersection(new ArrayList<>(set1), new ArrayList<>(set2));
        System.out.println("intersection = " + intersection);//intersection = [2, 3]

        /* 使用流的方式 判断 两个集合是否有交集 */
        System.out.println("set1.stream().anyMatch(set2::contains) = " +
                set1.stream().anyMatch(set2::contains));

    }

}
